package com.yeshine.opendata.controller.front.my.dataset;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.DataSet;
import com.yeshine.opendata.entity.DataTheme;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDataService;
import com.yeshine.opendata.service.IDataSetService;
import com.yeshine.opendata.service.IDataThemeService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.mybatis.DashboardMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by taohj on 2014/7/10.
 */
@Controller
@RequestMapping("/front/my/dataset")
public class MyDataSetController extends AbstractFrontControllerBase {


    @Autowired
    private IDataSetService service;

    @Autowired
    private IDataService dataService;


    @Autowired
    private IOrgService orgService;


    @Autowired
    private IDataThemeService dataThemeService;


    @RequestMapping("createV")
    public String createV(@RequestParam(value = "org", required = false) Integer org) {
        setRequestAttr("org", org);
        setRequestAttr("themes", dataThemeService.getList(null).getList());

        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("新增数据集", true));
        setRequestAttr("dashboardMenus", dashboardMenus);
        return "/front/my/dataset/create";
    }

    @RequestMapping("create")
    public String create(
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam("theme") Integer theme,
            @RequestParam(value = "org", required = false) Integer org
    ) {
        DataSet dataSet = new DataSet();
        dataSet.setName(name);
        dataSet.setDescription(description);
        dataSet.setCreater(getCurrentUser());
        dataSet.setTheme(new DataTheme(theme));
        if (org != null) {
            dataSet.setOrg(new Org(org));
        }

        Result<DataSet> result = service.create(dataSet);
        if (!result.isSuccess()) {
            setRequestForwordAttr("新增失败", result.getMessage(), "");
        }
        return "redirect:/front/my/dataset/list";
    }


    @RequestMapping("remove")
    public String remove(
            @RequestParam(value = "id", required = true) Integer id
    ) {
        Result<DataSet> result = service.remove(new DataSet(id));
        return getReturnString(result, "redirect:/front/my/dataset/list", setMessage("删除失败", "删除失败，请联系管理员"));

    }


    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam(value = "id", required = true) Integer id
    ) {
        DataSet dataset = service.getById(id).getTarget();
        setRequestAttr("dataset", dataset);
        setRequestAttr("themes", dataThemeService.getList(null).getList());
        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("更新数据集", true));
        setRequestAttr("dashboardMenus", dashboardMenus);
        return "front/my/dataset/modify";
    }

    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam("theme") Integer theme,
            @RequestParam(value = "org", required = false) Integer org
    ) {
        DataSet dataSet = service.getById(id).getTarget();
        dataSet.setId(id);
        dataSet.setName(name);
        dataSet.setTheme(new DataTheme(theme));
        dataSet.setDescription(description);
        dataSet.setOrg(new Org(org));
        Result<DataSet> result = service.modify(dataSet);
        return getReturnString(result, "redirect:/front/my/dataset/list", setMessage("修改失败", result.getMessage()));
    }


    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "org", required = false) Integer org,
            @RequestParam(value = "theme", required = false) Integer theme
    ) {
        PageResult<DataSet> pageResult = new PageResult<DataSet>();
        pageResult.setSize(size);
        pageResult.setPage(page);
        DataSet dataSet = new DataSet();
        dataSet.setCreater(getCurrentUser());
        if (theme != null) {
            dataSet.setTheme(new DataTheme(theme));
        }


        if (StringUtils.hasLength(name)) {
            dataSet.setName(name);
        }

        if (org != null) {
            dataSet.setOrg(new Org(org));
        }

        if (StringUtils.hasLength(order)) {
            pageResult.setSortBy(order);
            pageResult.setSortColumn("ds.name");
        }
        pageResult.setT(dataSet);
        pageResult.setWhere(" and ds.org is null ");
        Result<DataSet> result = service.getByPage(pageResult);
        setRequestAttr("result", result);
        setRequestAttr("name", name);
        return "/front/my/dataset/list";
    }


}
